package com.sopaths.dao;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import com.sopaths.contract.dao.RecruitmentDao;
import com.sopaths.contract.domain.Recruitment;
import com.sopaths.persistence.RecruitmentDetailMapper;
import com.sopaths.persistence.RecruitmentMapper;

@Named
public class RecruitmentDaoImpl implements RecruitmentDao {

	@Inject
	private RecruitmentMapper recruitmentMapper;

	@Inject
	private RecruitmentDetailMapper recruitmentDetailMapper;

	@Override
	public Recruitment create(Recruitment recruitment) {
		recruitmentMapper.create(recruitment);
		recruitmentDetailMapper.create(recruitment);
		return recruitment;
	}

	@Override
	public Recruitment delete(int id) {
		Recruitment recruitment = recruitmentMapper.get(id);
		if (recruitment != null) {
			int affectedRows = recruitmentMapper.delete(id);
			if (affectedRows > 0) {
				return recruitment;
			}
		}
		return null;
	}

	@Override
	public Recruitment update(Recruitment recruitment) {
		recruitmentMapper.update(recruitment);
		recruitmentDetailMapper.update(recruitment);
		return recruitment;
	}

	/**
	 * Retrieve recruitment by a specific id
	 * @return Recruitment
	 * @return null if not exist
	 */
	@Override
	public Recruitment get(int id) {
		return recruitmentMapper.get(id);
	}

	/**
	 * return the first id of the recruitment which matches the URL
	 * -1 if no recruitment matches the URL
	 */
	@Override
	public int getIdByUrl(String url) {
		List<Integer> ids = recruitmentMapper.getIdByUrl(url);
		if (ids == null || ids.size() == 0) {
			return -1;
		}
		return ids.get(0);
	}

	public void setRecruitmentMapper(RecruitmentMapper recruitmentMapper) {
		this.recruitmentMapper = recruitmentMapper;
	}

	public void setRecruitmentDetailMapper(
			RecruitmentDetailMapper recruitmentDetailMapper) {
		this.recruitmentDetailMapper = recruitmentDetailMapper;
	}
}
